﻿using System;

namespace GettingStartedWithRhinoMocks
{
    public class Customer
    {
        public string Name { get; set; }
    }

    public interface ICustomerValidator
    {
        bool IsValid(Customer customer);
    }

    public class CustomerValidator : ICustomerValidator
    {
        public const string CUSTOMER_NAME_ERROR_MESSAGE = "Customer name cannot be null or empty";

        public bool IsValid(Customer customer)
        {
            if (String.IsNullOrEmpty(customer.Name))
            {
                throw new ArgumentException(CUSTOMER_NAME_ERROR_MESSAGE);
            }
            return true;
        }
    }

    public interface ILogger
    {
        void LogError(string errorMessage);
    }
    
    public class CustomerService
    {
        private readonly ICustomerValidator _customerValidator;
        private readonly ILogger _logger;

        public CustomerService(ICustomerValidator customerValidator, ILogger logger)
        {
            _customerValidator = customerValidator;
            _logger = logger;
        }

        public bool AddCustomer(Customer customer)
        {
            try
            {
                if (_customerValidator.IsValid(customer))
                {
                    // Add it to the database
                    return true;
                }
            }
            catch (Exception exception)
            {
                _logger.LogError(exception.Message);
            }
            return false;
        }
    }
}